<template>
  <el-container>
    <!-- 顶部局域 -->
    <el-header class="top_container">
      <div>
        <img src="../assets/heima.png" />
        <span>电商管理系统</span>
      </div>
      <!-- 退出登录 -->
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollpase ? '64px' : '200px'">
        <div class="toggle_button" @click="isCollpase = !isCollpase">
          <i :class="isCollpase ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>
        <el-menu
          :default-active="activePath"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse-transition="false"
          :collapse="isCollpase"
          unique-opened
          router
        >
          <el-submenu
            :index="menu.id + ''"
            v-for="menu in menuList"
            :key="menu.id"
          >
            <template slot="title">
              <i :class="iconsObj[menu.id]"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <el-menu-item
              :index="`/${submenu.path}`"
              v-for="submenu in menu.children"
              :key="submenu.id"
              @click="selectMenuItem(`/${submenu.path}`)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ submenu.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      isCollpase: false,
      iconsObj: {
        // 一级菜单的icon图标
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      activePath: "",
    };
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取菜单栏数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      this.menuList = res.data;
    },
    selectMenuItem(activePath) {
      this.activePath = activePath;
      sessionStorage.setItem("active_path", activePath);
    },
  },
  created() {
    this.getMenuList();
    this.activePath = sessionStorage.getItem("active_path");
    if (!this.activePath) this.activePath = "/users";
  },
};
</script>

<style lang="less" scoped>
.top_container {
  background-color: #373d3f;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  div {
    display: flex;
    align-items: center;
    img {
      height: 45px;
      width: 45px;
    }
    span {
      margin-left: 15px;
    }
  }
}

.el-container,
.el-menu {
  height: 100%;
}
.toggle_button {
  background: #4a5064;
  height: 25px;
  color: white;
  display: flex;
  justify-content: end;
  align-items: center;
  padding-right: 15px;
  cursor: pointer;
}

.iconfont {
  padding-right: 10px;
}

.el-main {
  padding-bottom: 100px;
}
</style>